<template>
  <div class="chat-container">
    <div class="chat-frame">
      <el-scrollbar ref="scrollbar" class="chat-scrollbar" wrap-class="chat-scrollbar__wrap">
        <div>
          <div
            v-for="(message, index) in messages"
            :key="index"
            class="chat-messages"
          >
            <div v-if="message.type === 'user'" class="chat-message user-message">
              {{ message.content }}
            </div>
            <div v-else class="chat-message ai-message">
              {{ message.content }}
            </div>
          </div>
        </div>
      </el-scrollbar>
      
      <!-- 关键词按钮组 -->
      <div v-if="keywords.length > 0" class="keyword-buttons">
        <el-button
          v-for="keyword in keywords"
          :key="keyword"
          size="small"
          @click="sendKeyword(keyword)"
        >
          {{ keyword }}
        </el-button>
      </div>
    </div>
    
    <div class="chat-input">
      <el-input
        class="custom-textarea"
        resize="none"
        type="textarea"
        v-model="input"
        placeholder="请输入内容"
        :rows="4"
      ></el-input>
      <el-button type="primary" @click="sendMessage">
        <el-icon class="mr-1"><Position /></el-icon>发送
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue'
import { listAiChat, getAiChat, delAiChat, addAiChat, updateAiChat, postChat } from "@/api/system/aiChat";
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance();
const input = ref('');
const messages = ref([]);
const keywords = ref([]);
const type = ref('');
const scrollbar = ref(null)

    // 滚动到底部函数
    const scrollToBottom = () => {
      nextTick(() => {
        const scrollWrap = scrollbar.value.wrapRef; // 直接获取 el-scrollbar 的滚动容器
        if (scrollWrap) {
          scrollWrap.scrollTop = scrollWrap.scrollHeight;
        }
      });
    };

/** 发送消息 */
const sendMessage = async () => {
  if (!input.value.trim()) return;
  
  try {
    const response = await postChat({ input: input.value })
    const data = response.data || response
    
    messages.value.push({
      type: 'user',
      content: input.value
    })
     scrollToBottom(); // 滚动到底部    

    if (data.aiMessage) {
      messages.value.push({
        type: 'ai',
       content: data.aiMessage
      })
       scrollToBottom(); // 滚动到底部
    }
    
    keywords.value = data.keywords || []
    type.value = data.type || ''
    
    input.value = ''
  } catch (error) {
    console.error('Error:', error)
    ElMessage.error('请求失败：' + error.message)
  }
}

const sendKeyword = (keyword) => {
  let message = `我想用${keyword}${type.value}`;
  input.value = message;
  sendMessage();

   return {
      scrollbar, // 返回引用
    };
};
</script>

<style scoped>
.chat-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  width: 90%;
  min-height: 700px;
  margin: 0 auto;
  border-radius: 5px;
  gap: 20px;
}

.chat-frame {
  background-color: #f5f5f5;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  overflow: auto;
  width: 100%;
  max-width: 1400px;
  flex: 1;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.chat-scrollbar {
  flex: 1;
  padding: 20px;
}

.chat-messages {
  margin-bottom: 12px;
  
  
}

.chat-message {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 8px;
  margin: 8px 0;
  
}

.user-message {
  background: #e8f5fe;
  margin-left: auto;
}

.ai-message {
  background: bisque;
}

.chat-input {
  display: flex;
  align-items: flex-start;
  width: 100%;
  max-width: 1400px;
  height: 180px;
  padding: 10px 0;
  margin: 0 auto;
  gap: 12px;
}

.custom-textarea {
  flex: 1;
  height: 100%;
}

.chat-input .el-button {
  margin-top: 10px;
  height: 40px;
}

.keyword-buttons {
  padding: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  position: relative;
  background-color: #f5f5f5;
}
</style>
